<script setup>
import { ref, onMounted, computed } from 'vue'
import axios from 'axios'
import {ElMessage, ElMessageBox} from 'element-plus'
import {CirclePlus, GoodsFilled, Key, Refresh, Search} from '@element-plus/icons-vue'

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页参数
const pagination = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
})

// 查询参数
const queryParams = ref({
  goodsName: '',
  storageName: '',
  goodsTypeName: ''

})
// 全部数据
const allRecordList = ref([])
const recordList = ref([])

// 获取用户数据
const fetchUsers = async () => {
  loading.value = true
  try {
    const requestParams = {
      ...queryParams.value
    }

    const response = await axios.post('http://localhost:8080/record/listBySubquery', {
      pageNum: pagination.value.pageNum,
      pageSize: pagination.value.pageSize,
      param: requestParams
    })

    allRecordList.value = response.data.data
    // tableData.value = response.data.data
    pagination.value.total = response.data.total
    handleQuery();
  } catch (error) {
    console.error('请求失败:', error)
    ElMessage.error('数据加载失败')
  } finally {
    loading.value = false
  }
}

// 查询操作
const handleQuery = () => {
  const { goodsName, storageName, goodsTypeName } = queryParams.value

  // 过滤数据，allRe还是存储全部数据
  const filteredData = allRecordList.value.filter(item => {
    return (
        (!goodsName || item.goodsName.includes(goodsName)) &&
        (!storageName || item.storageName.includes(storageName)) &&
        (!goodsTypeName || item.goodsTypeName.includes(goodsTypeName))
    )
  })

  // 分页
  const start = (pagination.value.pageNum - 1) * pagination.value.pageSize
  const end = start + pagination.value.pageSize
  recordList.value = filteredData.slice(start, end)//当前页数据
  tableData.value = recordList.value
  pagination.value.total = filteredData.length
}

// 重置查询
const resetQuery = () => {
  queryParams.value = {
    goodsId: ''
  }
  handleQuery()
}

// 分页变化
const handlePageChange = (newPage) => {
  pagination.value.pageNum = newPage
  fetchUsers()
}



// 初始化时获取
onMounted(() => {
  fetchUsers()
})

// 计算总页数
const totalPages = computed(() => {
  return Math.ceil(pagination.value.total / pagination.value.pageSize)
})

</script>

<template>
  <el-main class="main-content">
    <el-scrollbar>
      <!-- 查询表单 -->
      <div class="query-form">
        <el-form :inline="true" :model="queryParams">
          <el-form-item label="货物名">
            <el-input
                v-model.trim="queryParams.goodsName"
                placeholder="请输入货物名"
                clearable
                @keyup.enter="handleQuery"
                class="search-input"
            >
              <template #prefix>
                <el-icon class="input-icon">
                  <GoodsFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="货物类型">
            <el-select
                v-model="queryParams.goodsTypeName"
                placeholder="请选择类型"
                clearable
                filterable
                class="search-select"
            >
              <el-option
                  v-for="(item, index) in allRecordList.filter(
                  (v, i, a) => a.findIndex(t => t.goodsTypeName === v.goodsTypeName) === i )"
                  :key="index + '_' + item.goodsTypeName"
                  :label="item.goodsTypeName"
                  :value="item.goodsTypeName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="所属仓库">
            <el-select
                v-model="queryParams.storageName"
                placeholder="请选择仓库"
                clearable
                filterable
                class="search-select"
            >
              <el-option
                  v-for="(item, index) in allRecordList.filter(
                  (v, i, a) => a.findIndex(t => t.storageName === v.storageName) === i )"
                  :key="index + '_' + item.storageName"
                  :label="item.storageName"
                  :value="item.storageName"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="action-buttons">
            <el-button
                type="primary"
                :loading="loading"
                @click="handleQuery"
                class="query-btn"
            >
              <el-icon class="mr-1"><Search /></el-icon>
              查询
            </el-button>
            <el-button
                @click="resetQuery"
                class="reset-btn"
            >
              <el-icon class="mr-1"><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据表格 -->
      <el-table
          :data="tableData"
          height="calc(100vh - 120px)"
          v-loading="loading"
          class="data-table"
      >
        <!-- 表格列定义 -->
        <el-table-column label="序号" width="80" type="index">
          <template #default="{ $index }">
            {{ (pagination.pageNum - 1) * pagination.pageSize + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="ID" width="140" v-if="false" /><!--隐藏-->
        <el-table-column prop="goodsName" label="货物名" width="120" />
        <el-table-column prop="goodsTypeName" label="货物类型" width="120" />
        <el-table-column prop="storageName" label="所属仓库" width="120" />
        <el-table-column prop="userName" label="申请人" width="120" />
        <el-table-column prop="adminName" label="操作管理员" width="120"/>
        <el-table-column prop="count" label="操作数量" width="100" />
        <el-table-column prop="createTime" label="操作时间" width="180" />
        <el-table-column prop="remark" label="备注" width="400" itemtype="text" />

        <!-- 分页 -->
        <template #append>
          <div class="pagination-wrapper" style="display: flex; align-items: center; gap: 16px;">
            <div class="pagination-info">
              共 {{ pagination.total }} 条，第 {{ pagination.pageNum }}/{{ totalPages }} 页
            </div>
            <el-pagination
                :current-page="pagination.pageNum"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                layout="prev, pager, next, ->, slot"
                @current-change="handlePageChange"
                class="custom-pagination single-page-pager"
                style="margin-top: 10px"
            >
              <div class="page-controls" style="display: flex; align-items: center; gap: 8px; ">
                <span class="current-page">跳转到</span>
                <el-select
                    v-model="pagination.pageNum"
                    placeholder="跳转至"
                    size="small"        style="width: 110px; margin-left: 10px"
                    @change="handlePageChange"
                >
                  <el-option
                      v-for="n in totalPages"
                      :key="n"
                      :label="`第 ${n} 页`"
                      :value="n"
                      style="text-align: center"
                  />
                </el-select>
              </div>
            </el-pagination>
          </div>
        </template>
      </el-table>
      <!-- 编辑对话框 -->
      <el-dialog v-model="dialogVisible" title="货物编辑" width="35%">
        <el-form :model="editForm" label-width="80px">
          <el-form-item label="货物名">
            <el-input v-model="editForm.name" clearable />
          </el-form-item>
          <el-form-item label="类型">
            <el-select
                v-model="editForm.typeId"
                placeholder="请选择类型"
                filterable
                clearable
            >
              <el-option
                  v-for="item in goodsTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="仓库">
            <el-select
                v-model="editForm.storageId"
                placeholder="请选择仓库"
                filterable
                clearable
            >
              <el-option
                  v-for="item in storageList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="editForm.count" clearable />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="editForm.remark" type="textarea" clearable />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitEdit">确认</el-button>
        </template>
      </el-dialog>

      <!-- 新增用户对话框 -->
      <el-dialog v-model="addDialogVisible" title="新增用户" width="35%">
        <el-form :model="addForm" label-width="80px">
          <el-form-item label="货物名" required>
            <el-input v-model.trim="addForm.name" clearable />
          </el-form-item>
          <el-form-item label="货物类型" required>
            <el-select
                v-model="addForm.typeId"
                placeholder="请选择类型"
                clearable
                filterable
            >
              <el-option
                  v-for="item in goodsTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="所属仓库" required>
            <el-select
                v-model="addForm.storageId"
                placeholder="请选择仓库"
                clearable
                filterable
            >
              <el-option
                  v-for="item in storageList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数量" required>
            <el-input v-model.number="addForm.count" clearable />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="addForm.remark" type="textarea" clearable />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAdd">确认</el-button>
        </template>
      </el-dialog>
    </el-scrollbar>
  </el-main>
</template>

<style scoped>
/* 主内容区 */
.main-content {
  padding: 0 !important;
  background: #f8fafc;
}

/* 查询表单 */
.query-form {
  padding: 16px 24px;
  background: #fff;
  margin: 16px;
  border-radius: 8px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

/* 输入框样式 */
.search-input {
  width: 240px;
}

:deep(.el-input__wrapper) {
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06) !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2) !important;
}

.input-icon {
  color: #64748b;
  margin-right: 8px;
}

/* 操作按钮 */
.action-buttons {
  margin-left: auto;
}

.query-btn,
.reset-btn {
  padding: 8px 20px;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.query-btn:hover {
  background-color: var(--el-color-primary); /* 使用主题颜色 */
  border-color: var(--el-color-primary); /* 使用主题颜色 */
}

.el-button--primary:hover {
  background-color: var(--el-color-primary); /* 使用主题颜色 */
  border-color: var(--el-color-primary); /* 使用主题颜色 */
}

.reset-btn:hover {
  color: #409EFF;
  border-color: #c6e2ff;
  background-color: #ecf5ff;
}

/* 数据表格 */
.data-table {
  margin: 0 16px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

:deep(.el-table__header th) {
  background-color: #f8fafc !important;
  font-weight: 600;
}
/* 对话框动画 */
.el-dialog {
  transition: all 0.3s ease-out;
}

/* 新增按钮样式 */
.add-btn {
  background-color: #67c23a;
  border-color: #67c23a;
  color: white;

  &:hover {
    background-color: #5daf34;
    border-color: #5daf34;
  }
}
.pagination-info {
  margin-left: 16px;
  font-size: 14px;
  margin-top: 10px;
}
</style>
